import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhaozeyang
 * @date 2018/11/2 16:09
 * @description
 */
public class 六十七 {

    //"10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101"
    //"110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"
    public static void main(String[] args) {
        System.out.println(addBinaryAll("11", "1"));
    }

    public static String addBinary(String a, String b) {
        long ha = Long.parseLong(a, 2);
        long hb = Long.parseLong(b, 2);
        long hc = ha + hb;
        String result = 求二进制.get(hc);
       /* String regPattern = "^(0+)";

        result = result.replaceAll(regPattern,"");*/
        String regPattern = "[0]*+";
        Pattern pattern = Pattern.compile(regPattern, Pattern.CASE_INSENSITIVE);
        // 去掉原始字符串开头位置的指定字符
        Matcher matcher = pattern.matcher(result);
        if (matcher.lookingAt()) {
            int end = matcher.end();
            result = result.substring(end);
        }
        return result;

    }

    private static String addBinaryAll(String a, String b) {
        String one = a.length() >= b.length() ? a : b;
        String two = a.length() < b.length() ? a : b;
        int add = 0;
        String result = "";
        int c = one.length() - two.length();
        for (int i = one.length() - 1; i >= 0; i--) {
            int aitem = Integer.valueOf(one.substring(i, i + 1));
            int j = i - c;
            int bitem = j <0 ? 0 : Integer.valueOf(two.substring(j, j + 1));

            int count = aitem + bitem + add;
            if (count / 2 > 0) {
                add = 1;
                result = String.valueOf(count % 2).concat(result);
            } else {
                add = 0;
                result = String.valueOf(count).concat(result);
            }
        }
        if (add > 0) {
            result = "1".concat(result);
        }
        return result;
    }


    public String addBinary123(String a, String b) {
        // Write your code here
        //若字符串a的长度小于字符串b的长度，两个字符串交换
        if(a.length() < b.length()){
            String temp = a;
            a =b;
            b = temp;
        }
        int la = a.length()-1;
        int lb = b.length()-1;
        int carries = 0;//进位
        String res = "";//结果值
        while(lb >= 0){//先依据短字符串的长度依次计算
            int sum = (int)(a.charAt(la)-'0')+(int)(b.charAt(lb)-'0')+carries;
            res = String.valueOf(sum%2)+res;
            carries =sum/2;
            la--;
            lb--;
        }
        while(la>=0){//再依据长字符串的长度依次计算
            int sum = (int)(a.charAt(la)-'0')+carries;
            res = String.valueOf(sum%2)+res;
            carries =sum/2;
            la--;
        }

        if (carries==1){
            res ="1"+res;//计算最后的进位
        }
        return res;
    }
}
